if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[RemoveLogItemsForCOI]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[RemoveLogItemsForCOI]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/***************************************************************************
* Name:			RemoveLogItemsForCOI
* Purpose:		Delete log items
*
* PARAMETERS
* Name				Description					
* -------------		-----------------------------------------------------------
@protocolProcessId	COI id for the logItem to be deleted


***************************************************************************/
CREATE PROCEDURE [dbo].[RemoveLogItemsForCOI]
(
@coiId int
)

AS BEGIN

SET NOCOUNT ON
	DECLARE @TempLogItemIds TABLE(
	[LogItemId] [int] NOT NULL
	)

	INSERT INTO @TempLogItemIds (LogItemId)
	SELECT LogItemId 
	FROM COI_LogItem_Map
	WHERE COIId = @coiId

	DELETE FROM COI_LogItem_Map WHERE COIId = @coiId

	DELETE FROM LogItem Where 
	Id in (Select LogItemId from @TempLogItemIds)

	IF @@Error<>0 
	RAISERROR('Unable to remove LogItems for the specfied COI', 16, 1)
	RETURN

END

